function [f] = hc_obj(z)
% Implements the nonlinear hanging chain objective
% Note that only intermediate mass points have degree of freedom

% We assume that z =
%  [x1, x2, ..., xN, y1, ..., yN]'

% We reshape z to be easier to evaluate, each row will contain one
% coordinate pair (xi, yi)
zr = reshape(z, length(z) / 2, 2);
N = size(zr, 1);

% Definining constants
L = 0;
D = 15;
m = 1;
g = 9.81;
LoN = L / N;
% z1 = [-1, 1];
% zN = [2, 2];

f = 0;
for k = 1 : N - 1
   f = f + D * ( norm(zr( k + 1, :) - zr(k,:)) - LoN)^2 + m * g * zr(k, 2);
end
f = f + zr(end, 2) * m * g;

% Adding potential of fixed balls (we have an unconstrained problem
%f = f + D * (norm(zr(1, :) - z1) - LoN)^2;
%f = f + D * (norm(zN - zr(end, :)) - LoN)^2;


   